Skip to content

fix(extension): return structured errors instead of throwing in command handlers#319

Closed
margaretjgu wants to merge 22 commits into
mainfrom
fix/extension-handler-errors
Closed

fix(extension): return structured errors instead of throwing in command handlers#319
margaretjgu wants to merge 22 commits into
mainfrom
fix/extension-handler-errors

Conversation

@margaretjgu
Copy link
Copy Markdown
Member

Summary

Fixes #312.

installExtension, uninstallExtension, upgradeExtension, and searchExtensions can throw on invalid input (bad source string, extension not found, network error). The factory only converts { error: { code } } return values to clean user-facing messages -- unhandled throws produced a raw Node.js stack trace.

Before:

file:///dist/extension/installer.js:69
        throw new Error(`Invalid GitHub source "bad-source"...`)
               ^
Error: Invalid GitHub source "bad-source"...
    at parseSource (installer.js:69)
    at installExtension (installer.js:190)
    ...
Node.js v24.9.0

After:

Error: Invalid GitHub source "bad-source". Use github:owner/repo or owner/repo.

Changes

  • src/extension/register.ts: added a handlerError() helper and wrapped handleInstall, handleRemove, handleUpgrade, and handleSearch in try-catch blocks that return structured { error: { code, message } } objects
  • test/extension/register.test.ts: new test file covering the error-handling paths for install (bad source), upgrade (not installed), and confirming no stack traces leak to stderr

Note

This branch is based on feat/extension-search-upgrade (PR #304) since the extension files are not yet on main. It should be merged after the extension chain lands.

Test plan

  • returns structured error for invalid source instead of throwing -- new
  • returns structured error for bad github: prefix -- new
  • returns structured error for bare single-segment source -- new
  • returns structured error when named extension is not installed -- new

…nd handlers

installExtension, uninstallExtension, upgradeExtension, and searchExtensions
can throw on invalid input or missing extensions. The factory only converts
{ error: { code } } return values to clean user-facing messages -- unhandled
throws produce a raw Node.js stack trace.

Wrap all four handler functions in try-catch and return handlerError() so
errors are displayed as 'Error: <message>' with a non-zero exit code rather
than crashing with a full stack trace.

Fixes #312
@github-actions
Copy link
Copy Markdown

MegaLinter analysis: Error

Descriptor Linter Files Fixed Errors Warnings Elapsed time
✅ COPYPASTE jscpd yes no no 6.77s
✅ REPOSITORY gitleaks yes no no 54.92s
✅ REPOSITORY git_diff yes no no 0.93s
✅ REPOSITORY secretlint yes no no 29.61s
✅ REPOSITORY trivy yes no no 17.98s
❌ TYPESCRIPT eslint 11 2 0 5.18s

Detailed Issues

❌ TYPESCRIPT / eslint - 2 errors
test/extension/register.test.ts
   38:7   error  The value assigned to 'exitCode' is not used in subsequent statements  no-useless-assignment
  103:15  error  'stderr' is assigned a value but never used                            @typescript-eslint/no-unused-vars

✖ 2 problems (2 errors, 0 warnings)

See detailed reports in MegaLinter artifacts
Set VALIDATE_ALL_CODEBASE: true in mega-linter.yml to validate all sources, not only the diff

MegaLinter is graciously provided by OX Security
Show us your support by starring ⭐ the repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(extension): unhandled exception on invalid install source

1 participant